package com.xw.userService.web;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.xw.userService.dto.ResultDto;
import com.xw.userService.dto.request.UserInfoRequestDTO;
import com.xw.userService.dto.response.UserInfoResponseDTO;
import com.xw.userService.enums.ResultCodeEnum;
import com.xw.userService.model.UserInfo;
import com.xw.userService.service.UserInfoService;
import com.xw.userService.service.impl.UserInfoServiceImp;

/**
 * 用户接口
 * @author xiaoshengda
 *
 * @date 创建时间：2018年4月21日 下午5:13:01
 */
@RestController
@RequestMapping("/user/service")
public class UserInfoController {
	
	private static final Logger LOG = LoggerFactory.getLogger(UserInfoServiceImp.class);
    @Autowired
    private UserInfoService userInfoService;
	
	@RequestMapping(value = "/register", method = RequestMethod.POST)
	@HystrixCommand(fallbackMethod = "registerFB")
	public ResultDto<?> register(@RequestBody UserInfoRequestDTO userInfoRequestDTO) {
		ResultDto<?> resultDto=new ResultDto<>();
		LOG.info("service注册用户入参register:"+userInfoRequestDTO.toString());
		UserInfo userInfo=new UserInfo();
		BeanUtils.copyProperties(userInfoRequestDTO, userInfo);
		resultDto=this.userInfoService.insert(userInfo);
		LOG.info("service注册用户响应参数register:"+resultDto.toString());
		return resultDto;

	}
	
	/**
	 * 注册失败时回调方法
	 * @param userInfoRequestDTO
	 * @return    参数
	 * @author:xiaoshengda
	 * @date 创建时间：2018年4月21日 下午5:10:42
	 */
	public ResultDto<?> registerFB(@RequestBody UserInfoRequestDTO userInfoRequestDTO) {
		LOG.info("注册用户回调入参register:"+userInfoRequestDTO.toString());
		ResultDto<UserInfoResponseDTO> resultDto = new ResultDto<>();
		resultDto.setCode(ResultCodeEnum.System.SYSTEM_ERROR.getCode());
		resultDto.setCode(ResultCodeEnum.System.SYSTEM_ERROR.getMsg());
		return resultDto;

	}
}
